package com.incross.dawin.views;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.OrientationEventListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.incross.dawin.util.CommonUtils.ScreenSize;

public class BKBrowserDialog extends Dialog implements android.view.View.OnClickListener {
	
	private Context mContext;
	private int mTheme;
	
	private WebView mWebView;
	private ImageButton mBackButton, mForwardButton, mRefreshButton, mShareButton, mCloseButton;
	
	private String mUrl;
	
	private ScreenSize mScreenSize;
	
	private OnDialogStateListener mListener;
	
	
	public interface OnDialogStateListener {
		public abstract void onDialogDismissed();
		public abstract void onDialogShown();
	}
	
	
	
	public BKBrowserDialog(Context context, String url) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		
		mContext = context;
		mUrl = url;
		setDialogView();
	}
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}
	
	private void setDialogView() {
		
		Window window = getWindow();
		window.setGravity(Gravity.CENTER);
		
		LayoutParams param = window.getAttributes();
		param.width = LayoutParams.MATCH_PARENT;
		param.height = LayoutParams.MATCH_PARENT;
		
		
		
		window.setAttributes((android.view.WindowManager.LayoutParams) param);
		
		
		
		
		LinearLayout mainLayout = new LinearLayout(mContext);
		mainLayout.setOrientation(LinearLayout.VERTICAL);
		
		LinearLayout.LayoutParams mainParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, 1.0f);
		
		
		
		
		// Web view area
		mWebView = new WebView(mContext);
		mWebView.getSettings().setBuiltInZoomControls(false);
		mWebView.getSettings().setJavaScriptEnabled(true);
		// 모바일인지 데스크탑인지 구분, 0 - 모바일, 1 - 데스크탑
		mWebView.getSettings().setUserAgent(0);
		
		// 화면크기 맞춤
		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.setInitialScale(1);
		
		// 브라우저 실행 안되게 막는다
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
	        public boolean shouldOverrideUrlLoading(WebView view, String url) {
	            return (false);
	        }
		});
		
		LinearLayout.LayoutParams webViewParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 0, 1.0f);
		mWebView.setBackgroundColor(Color.parseColor("#ffffff"));
		
		
		// Bottom button area
		LinearLayout buttonLayout = new LinearLayout(mContext);
		buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
		
		LinearLayout.LayoutParams buttonLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 150, 0f);
		
		LinearLayout.LayoutParams buttonParam = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.FILL_PARENT, 1.0f);
		
		mBackButton = new ImageButton(mContext);
		mBackButton.setOnClickListener(this);
		mForwardButton = new ImageButton(mContext);
		mForwardButton.setOnClickListener(this);
		mRefreshButton = new ImageButton(mContext);
		mRefreshButton.setOnClickListener(this);
		mShareButton = new ImageButton(mContext);
		mShareButton.setOnClickListener(this);
		mCloseButton = new ImageButton(mContext);
		mCloseButton.setOnClickListener(this);
		
		buttonLayout.addView(mBackButton, buttonParam);
		buttonLayout.addView(mForwardButton, buttonParam);
		buttonLayout.addView(mRefreshButton, buttonParam);
		buttonLayout.addView(mShareButton, buttonParam);
		buttonLayout.addView(mCloseButton, buttonParam);
		
		
		mainLayout.addView(mWebView, webViewParam);
		mainLayout.addView(buttonLayout, buttonLayoutParam);
		
		
		setContentView(mainLayout, mainParam);
	}
	
	public void setOnDialogStateListener(OnDialogStateListener l) {
		mListener = l;
	}
	
	@Override
	public void show() {
		
		super.show();
		if(mListener != null) {
			mListener.onDialogShown();
		}
		
		mWebView.loadUrl(mUrl);
		
	}
	
	@Override
	public void dismiss() {
		if(mListener != null) {
			mListener.onDialogDismissed();
		}
		super.dismiss();
	}

	
	@Override
	public void onClick(View v) {
		if(v.equals(mBackButton)) {
			Toast.makeText(mContext, "Back button", Toast.LENGTH_SHORT).show();
		} else if(v.equals(mForwardButton)) {
			Toast.makeText(mContext, "Forward button", Toast.LENGTH_SHORT).show();
		} else if(v.equals(mRefreshButton)) {
			Toast.makeText(mContext, "Refresh button", Toast.LENGTH_SHORT).show();
		} else if(v.equals(mShareButton)) {
			Toast.makeText(mContext, "Share button", Toast.LENGTH_SHORT).show();
		} else if(v.equals(mCloseButton)) {
			this.dismiss();
		}
		
	}
	
	
	

}
